/**
 * 自定义封装前段组件和常用的方法 常量
 * 只为了更快速、更少代码的去开发layui前端页面
 * @author AIFast
 */
var default_option = {
    pid: '', //组件外层的div id
    id: 'demo',
    formName: 'demo',
    type: 'input', //组件的类型 支持html常用 input radio checkbox select div等
    title: '',
    tips: '',
    notNull: false,
    initValue: '', //回显的值 没有不填
}

var default_select_Option = {
    pid: '',
    id: undefined,
    data: [],
    name: undefined,
    value: undefined,
    hint: '请选择',
    initValue: undefined,
    method: 'get',
    where: undefined,
    headers: undefined,
    async: true,
    done: undefined,
    error: undefined
};

String.prototype.strFormat = function () {
    if (arguments.length === 0) return this;
    var param = arguments[0], str = this;
    if (typeof (param) === 'object') {
        for (var key in param)
            str = str.replace(new RegExp("\\{" + key + "\\}", "g"), param[key]);
        return str;
    } else {
        for (var i = 0; i < arguments.length; i++)
            str = str.replace(new RegExp("\\{" + i + "\\}", "g"), arguments[i]);
        return str;
    }
}

var Ai = {
    /**实例化一个组件*/
    new: function (option) {
        if (!option) {
            option = default_option;
        }
        if (!option.pid) {
            console.log('元素外层div的id值不能为空');
        }
        if (option.notNull) {
            option.notNull = 'layui-form-required';
        } else {
            option.notNull = ''
        }
        let comp = '';
        switch (option.type) {
            case 'input':
                comp = '<div class="layui-inline"><label class="layui-form-label {notNull}">{title}</label><div class="layui-input-inline"><input class="layui-input" name="{formName}" placeholder="{tips}" placeholder="{tips}" /></div></div>'.strFormat(option);
                break;
            case 'input_num':
                comp = '<div class="layui-inline"><label class="layui-form-label {notNull}">{title}</label><div class="layui-input-inline"><input class="layui-input" type="number" name="{formName}" placeholder="{tips}" placeholder="{tips}" /></div></div>'.strFormat(option);
                break;
            case 'input_pass':
                comp = '<div class="layui-inline"><label class="layui-form-label {notNull}">{title}</label><div class="layui-input-inline"><input class="layui-input" type="password" name="{formName}" placeholder="{tips}" placeholder="{tips}" /></div></div>'.strFormat(option);
                break;
            case 'select':
                option = layui.jquery.extend(default_select_Option, option);
                if (typeof option.data === 'string') {
                    layui.jquery.ajax({
                        url: option.data,
                        type: option.method,
                        data: option.where,
                        dataType: 'json',
                        headers: option.header || option.headers,
                        async: option.async,
                        success: function (result, status, xhr) {
                            if (result.data) {
                                option.data = result.data;
                                Ai.new(option);
                            } else {
                                option.error && option.error(xhr, result);
                            }
                        },
                        error: option.error
                    });
                } else {
                    var html = option.hint ? ('<option value="">' + option.hint + '</option>') : '';
                    for (var i = 0; i < option.data.length; i++) {
                        if (option.name && option.value) {
                            html += ('<option value="' + option.data[i][option.value] + '"' + (option.data[i][option.value] == option.initValue ? ' selected' : '') + '>' + option.data[i][option.name] + '</option>');
                        } else {
                            html += ('<option value="' + option.data[i] + '"' + (option.data[i] == option.initValue ? ' selected' : '') + '>' + option.data[i] + '</option>');
                        }
                    }
                    comp = '<div class="layui-inline"><label class="layui-form-label">{title}</label><div class="layui-input-inline"><select id="{id}" name="{formName}" lay-filter="{formName}" >'.strFormat(option);
                    comp += html;
                    comp += '</select></div></div>';
                    // layui.jquery(option.id).html(html);
                    /*var $form = layui.jquery(option.id).parent('.layui-form');
                    if ($form.length === 0) {
                        $form = layui.jquery(option.id).parentsUntil('.layui-form').last().parent();
                    }*/

                    option.done && option.done(option.data);
                    layui.form.render('select');
                }
                // comp = '<div class="layui-form-item"><label class="layui-form-label">{title}</label>';

                break;
            case 'input_number':
                break;
            case 'input_number':
                break;
            case 'input_number':
                break;
            case 'input_number':
                break;
            default:
                comp = '<div class="layui-inline"><label class="layui-form-label {notNull}">{title}</label><div class="layui-input-inline"><input class="layui-input" name="{name}" placeholder="{tips}" placeholder="{tips}" /></div></div>'.strFormat(option);
        }

        layui.jquery('#' + option.pid).append(comp);


    }

}